<?php
namespace app\erp\model;

use think\Model;
use traits\model\SoftDelete;
use app\erp\validate\Partnerrule;
use app\erp\controller\Partner;

class Partnerdb extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    protected $pk = 'partner_id';

    protected $name = 'partner';

    protected $type = [
        'start_time' => 'timestamp:Y-m-d',
        'end_time' => 'timestamp:Y-m-d'
    ];
    // 查询名称
    public function scopePartnerName($query, $partnerName)
    {
        $query->where('partner_name', 'like', '%' . $partnerName . '%');
    }
    // 查询结束时间
    public function scopeEndTime($query, $endTime)
    {
        $temp = explode(' - ', $endTime);
        $query->whereTime('end_time', $temp);
    }
    // 查询开始时间
    public function scopeStartTime($query, $startTime)
    {
        $temp = explode(' - ', $startTime);
        $query->whereTime('start_time', $temp);
    }
    // 查询买断
    public function scopeMaiduan($query, $maiduan)
    {
        if ($maiduan == 1) {
            $query->where('end_time', 0);
        } else {
            $query->where('end_time', '>', 0);
        }
    }
    // 查询数据转换
    public function ajaxSelect($param)
    {
        if (isset($param['partner_name']) && ! empty($param['partner_name'])) {
            $this->partnerName($param['partner_name']);
        }
        if (isset($param['start_time']) && ! empty($param['start_time'])) {
            $this->startTime($param['start_time']);
        }
        if (isset($param['end_time']) && ! empty($param['end_time'])) {
            $this->endTime($param['end_time']);
        }
        if (isset($param['maiduan']) && $param['maiduan'] > 0) {
            $this->maiduan($param['maiduan']);
        }
        $data = $this->order($param['_o'], $param['_b'])
            ->page($param['page'], $param['limit'])
            ->select();
        $count = $this->count();
        if (! $data->isEmpty()) {
            $data = $data->toArray();
            foreach ($data as $key => $val) {
                $data[$key] = $this->handleData($val);
            }
        }
        
        $return['data'] = $data;
        $return['count'] = $count;
        return $return;
    }

    /**
     *
     * @todo 获取合作伙伴详情
     * @param unknown $id            
     */
    public function ajaxFind($partner_id)
    {
        $data = $this->where('partner_id', $partner_id)->find();
        $data = $data->toArray();
        $data = $this->handleData($data);
        return $data;
    }

    /**
     *
     * @todo 处理数据
     */
    protected function handleData($val)
    {
        foreach ($val as $k => $v) {
            switch ($k) {
                case 'end_time':
                    if ($v == '1970-01-01') {
                        $val[$k] = '买断';
                    } else {
                        $val[$k] = $v;
                    }
                    break;
                default:
                    $val[$k] = $v;
                    break;
            }
        }
        return $val;
    }
    
    // 软删除
    public function ajaxDelete($param)
    {
        if (isset($param['partner_id'])) {
            $this->destroy($param['partner_id']);
        }
    }
    // 保存数据
    public function ajaxSave($param)
    {
        if (isset($param['maiduan']) && $param['maiduan'] == 'on') {
            $param['end_time'] = 0;
        }
        // 验证
        $rule = new Partnerrule();
        if (! $rule->check($param)) {
            return $rule->getError();
        }
        if (isset($param['partner_id']) && $param['partner_id'] != '') {
            $partner_id = $param['partner_id'];
            unset($param['partner_id']);
            $this->save($param, [
                'partner_id' => $partner_id
            ]);
        } else {
            $this->allowField(true)->save($param);
        }
        
        return 'success';
    }
}